import Vue from 'vue'
/* 小程序路由跳转 */
import qs from 'uview-ui/libs/function/queryParams'
import { getAuthToken } from '@/api/system'
import { setRichText, getEnv } from '@/utils/buffer'
import { actionScheme, qsScheme } from '@/utils/scheme'
import { copyData, getProjectTypeById } from './index'

export default {
  /**
   * 跳转登录
   */
  navigateToLogin: (params, success) => {
    uni.navigateTo({
      url: `/packages/User/Login/index${ qs(params) }`,
      success
    })
  },
  redirectToLogin: (params, success) => {
    uni.redirectTo({
      url: `/packages/User/Login/index${ qs(params) }`,
      success
    })
  },
  relaunchToLogin: (params, success) => {
    uni.reLaunch({
      url: `/packages/User/Login/index${ qs(params) }`,
      success
    })
  },
  /**
   * 项目搜索页
   */
  navigateToSearch: (success) => {
    uni.navigateTo({
      url: `/packages/Project/Search/index`,
      success
    })
  },
  /**
   * 项目搜索结果页
   * @param {String} key 搜索关键词
   * @param {Object} params 其他参数
   */
  navigateToSearchResult: (key, params = {}, success) => {
    uni.navigateTo({
      url: `/packages/Project/SearchResult/index${ qs({ key, ...params }) }`,
      success
    })
  },
  /**
   * 项目列表页
   */
  navigateToProjects: (projectType = 'competition', params = {}) => {
    actionScheme({
      scheme: qsScheme({
        scheme: 'projects',
        projectType,
        ...params
      }),
    })
  },
  /**
   * 项目详情
   * @param {Number} id
   */
  navigateToProject: (id, params = {}) => {
    actionScheme({
      scheme: qsScheme({
        scheme: 'project',
        id,
        projectType: getProjectTypeById(id),
      }),
      ...params
    })
  },
  /**
   * 创建项目
   */
  navigatorMyProject: () => {
    uni.navigateTo({
      url: `/packages/Project/MyProjects/index`
    })
  },
  /**
   * 创建项目
   * @param {String} type 类型 competition/courseDesign
   */
  navigatorCreateProject: async(type) => {
    /* web创建还是小程序创建 */
    const res = await Vue.prototype.$menuPicker([
      { label: '小程序创建', cb: () => {
        uni.navigateTo({
          url: `/packages/Project/EditProject/Create/index?projectType=${ type }`
        })
      } },
      { label: '浏览器创建', cb: () => {
        const url = `${ getEnv().baseUrl }/ahaWeb/#/project/create/${ type }`

        copyData(url, '已复制链接,请使用浏览器打开')
      } }
    ])

    res.cb()
  },
  /**
   * 编辑项目
   * @param {String} type 类型 competition/courseDesign
   * @param {String} id id
   */
  navigateToEditProject: async(type, id) => {
    const res = await Vue.prototype.$menuPicker([
      { label: '小程序编辑', cb: () => {
        uni.navigateTo({
          url: `/packages/Project/EditProject/Update/index?id=${ id }&projectType=${ type }`
        })
      } },
      { label: '浏览器编辑', cb: () => {
        const url = `${ getEnv().baseUrl }/ahaWeb/#/project/edit/${ id }`

        copyData(url, '已复制链接,请使用浏览器打开')
      } }
    ])

    res.cb()
  },
  /**
   * 编辑项目
   * @param {String} type 类型 competition/courseDesign
   * @param {String} id id
   */
  redirectToEditProject: (type, id) => {
    uni.redirectTo({
      url: `/packages/Project/EditProject/Update/index?id=${ id }&projectType=${ type }`
    })
  },
  /**
   * 前往用户主页
   */
  navigateToUserHome: (id) => {
    actionScheme({
      scheme: qsScheme({
        scheme: 'userHome',
        id
      }),
    })
  },
  /**
   * 前往用户列表
   * @param {String} type 列表类型， normal普通, follower粉丝, bean 关注, give 赠送列表
   */
  navigateToUserList: (type = 'normal') => {
    actionScheme({
      scheme: qsScheme({
        scheme: 'userList',
        type
      }),
    })
  },
  /**
   * 前往我的详细资料
   */
  navigateToMyInfo: (success) => {
    uni.navigateTo({
      url: `/packages/User/MyInfo/index`,
      success
    })
  },
  /**
   * 前往用户反馈
   */
  navigateToFeedback: (success) => {
    uni.navigateTo({
      url: '/packages/User/Feedback/Submit',
      success
    })
  },
  /**
   * 前往咨询列表
   */
  navigateToConsultList: () => {
    actionScheme({
      scheme: qsScheme({
        scheme: 'consultList',
      }),
    })
  },
  /**
   * 前往咨询详情
   */
  navigateToConsultDetail: (siId) => {
    actionScheme({
      scheme: qsScheme({
        scheme: 'consultDetail',
        siId
      }),
    })
  },
  /**
   * 前往咨询评价列表
   */
  navigateToConsultComments: (siId) => {
    uni.navigateTo({
      url: `/packages/Consult/CommentList/index?siId=${ siId }`
    })
  },
  /**
   * 前往咨询付款
   */
  navigateToConsultPay: (siId) => {
    uni.navigateTo({
      url: `/packages/Consult/Process/Pay/index?siId=${ siId }`
    })
  },
  /**
   * 前往咨询问题填写
   */
  navigateToConsultQuestion: (scpId) => {
    uni.navigateTo({
      url: `/packages/Consult/Process/Question/index?scpId=${ scpId }`
    })
  },
  redirectToConsultQuestion: (scpId) => {
    uni.redirectTo({
      url: `/packages/Consult/Process/Question/index?scpId=${ scpId }`
    })
  },
  /**
   * 前往咨询会议页面
   */
  navigateToConsultMeeting: (scpId) => {
    uni.navigateTo({
      url: `/packages/Consult/Process/Meeting/index?scpId=${ scpId }`
    })
  },
  redirectToConsultMeeting: (scpId) => {
    uni.redirectTo({
      url: `/packages/Consult/Process/Meeting/index?scpId=${ scpId }`
    })
  },
  /**
   * 前往咨询订单评价
   */
  navigateToConsultComment: (siId) => {
    uni.navigateTo({
      url: `/packages/Consult/Process/Comment/index?siId=${ siId }`
    })
  },

  /**
   * 前往竞赛列表
   */
  navigateToCompetitions: () => {
    actionScheme({
      scheme: 'competitionList'
    })
  },

  /**
   * 跳转富文本查看页
   */
  navigateToRichText: (intro, name) => {
    if (intro) {
      setRichText(intro, name)
      uni.navigateTo({
        url: '/packages/System/RichText/index'
      })
    }
  },
  /**
   * 跳转富文本编辑
   * @param {Boolean} 是否是复制操作，管理员专用
   */
  navigateToEdit: (copy = false) => {
    uni.navigateTo({
      url: `/packages/System/EditMd/EditMd?copy=${ copy }`
    })
  },
  /**
   * 跳转webview
   * @param {String} url 外部链接
   * @param {Boolean} isLogin 是否需要登录
   */
  navigateToWebview: async(url, isLogin = false) => {
    let authToken = null

    let newUrl = url
    /* 判断是否需要登录，需要登录生成临时token */

    if (isLogin) {
      authToken = await getAuthToken()
      if (url.includes('?')) {
        newUrl = `${ url }&token=${ authToken }`
      } else {
        newUrl = `${ url }?token=${ authToken }`
      }
    }

    uni.navigateTo({
      url: `/packages/System/WebView/index${ qs({ url: encodeURIComponent(newUrl) }) }`
    })
  },
  /**
   * 跳转问卷星
   * @param {String} path 文件路径
   */
  navigateToQuestionnaire: (path) => {
    actionScheme({
      scheme: qsScheme({
        scheme: 'questionnaire',
        path
      })
    })
  },
  /**
   * 后退
   * @param {String} msg 成功后提示文字
   * @param {Number} page 后退页数
   */
  navigateBack: (msg = '', page = 1) => {
    const pageLen = getCurrentPages().length

    if (pageLen === 1) {
      actionScheme({ scheme: 'home', closePage: true, complete: () => {
        if (msg) {
          Vue.prototype.$msg(msg)
        }
      } })
    } else {
      uni.navigateBack({
        delta: page,
        complete: () => {
          if (msg) {
            Vue.prototype.$msg(msg)
          }
        }
      })
    }
  }
}
